<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="index_style.css"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form method="POST" action="BlogMain.php">
            <input type="submit" name="blogmain" value="Back to blog main"/>
        </form>
        <form method="POST">
            <input type="submit" name="logout" value="Log out"/>
        </form>
        <?php
        session_start();

        if (!isset($_SESSION['userid']))
        {
            echo"User not logged in";
            echo"</html>";
            exit();
        }
        if (isset($_POST['logout']))
        {
            include 'ClassLibs/statics.php';
            vLogout();
            header("Location: index.php");
        }

        if (isset($_POST['delete']) && isset($_POST['pdfid']))
        {
            include 'ClassLibs/statics.php';
            if (isset($_SESSION['author_name']))
            {
                $_POST['author_name'] = $_SESSION['author_name'];
            }
            else if (isset($_SESSION['date']))
            {
                $_POST['date'] = $_SESSION['date'];
            }
            vDeletePdf($_POST['pdfid']);
        }
        else if (isset($_POST['delete']) && isset($_POST['textid']))
        {
            include 'ClassLibs/statics.php';
            if (isset($_SESSION['author_name']))
            {
                $_POST['author_name'] = $_SESSION['author_name'];
            }
            else if (isset($_SESSION['date']))
            {
                $_POST['date'] = $_SESSION['date'];
            }
            vDeleteText($_POST['textid']);
        }
        if (isset($_POST['author_name']))
        {
            $_SESSION['author_name'] = $_POST['author_name'];
            unset($_SESSION['date']);
            include 'ClassLibs/statics.php';
            $rsResultPdf = rsPdfSearchByAuthor($_POST['author_name']);
            $rsResultText = rsTextSearchByAuthor($_POST['author_name']);
            if ($_POST['author_name'] != $_SESSION['userid'])
            {
                echo"<h1>Pdf posts</h1>
                <table border=\"1\">
                <tr>
                <td>ID</td><td>Author name</td><td>File name</td><td>File type</td>
                <td>File size</td><td>Date</td><td>View</td>
                </tr>
                ";
                while ($row = mysql_fetch_assoc($rsResultPdf))
                {
                    echo "
                <tr>
                    <td>{$row['ID']}</td><td>{$row['UserName']}</td><td>{$row['FileName']}</td>
                    <td>{$row['FileType']}</td><td>{$row['FileSize']}</td><td>{$row['Date']}</td>
                   <form method=\"POST\" target=\"_blank\" action=\"ActionPdf.php\">
                    <input type=\"hidden\" name=\"pdfid\" value=\"{$row['ID']}\"/>
                    <td><input type=\"submit\" name=\"view\" value=\"View\"/></td>  
                   </form>
               </tr>
               ";
                }
                echo"</table><br/>";

                echo"<h1>Text posts</h1>
                <table border=\"1\">
                <tr>
                <td>ID</td><td>Author name</td><td>Date</td><td>View</td>
                </tr>
                ";
                while ($row = mysql_fetch_assoc($rsResultText))
                {
                    echo "
                <tr>
                    <td>{$row['ID']}</td><td>{$row['UserName']}</td><td>{$row['Date']}</td>
                 <form method=\"POST\" target=\"_blank\" action=\"ActionText.php\">
                    <input type=\"hidden\" name=\"textid\" value=\"{$row['ID']}\"/>
                    <td><input type=\"submit\" name=\"view\" value=\"View\"/></td>
                 </form>
               </tr>
               ";
                }
                echo"</table>";
            }
            else if ($_POST['author_name'] === $_SESSION['userid'])
            {
                echo"<h1>Pdf posts</h1>
                <table border=\"1\">
                <tr>
                <td>ID</td><td>Author name</td><td>File name</td><td>File type</td>
                <td>File size</td><td>Date</td><td>View</td><td>Delete</td>
                </tr>
                ";
                while ($row = mysql_fetch_assoc($rsResultPdf))
                {
                    echo "
                <tr>
                    <td>{$row['ID']}</td><td>{$row['UserName']}</td><td>{$row['FileName']}</td>
                    <td>{$row['FileType']}</td><td>{$row['FileSize']}</td><td>{$row['Date']}</td>
                <form method=\"POST\" target=\"_blank\" action=\"ActionPdf.php\">
                  <input type=\"hidden\" name=\"pdfid\" value=\"{$row['ID']}\"/>
                  <td><input type=\"submit\" name=\"view\" value=\"View\"/></td>  
                </form>
                <form method=\"POST\">
                  <input type=\"hidden\" name=\"pdfid\" value=\"{$row['ID']}\"/>
                  <td><input type=\"submit\" name=\"delete\" value=\"Delete\"/></td>
                  </form>
               </tr>
               ";
                }
                echo"</table><br/>";

                echo"<h1>Text posts</h1>
                <table border=\"1\">
                <tr>
                <td>ID</td><td>Author name</td><td>Date</td><td>View</td>
                <td>Delete</td>
                </tr>
                ";
                while ($row = mysql_fetch_assoc($rsResultText))
                {
                    echo "
                <tr>
                    <td>{$row['ID']}</td><td>{$row['UserName']}</td><td>{$row['Date']}</td>
                <form method=\"POST\" target=\"_blank\" action=\"ActionText.php\">
                    <input type=\"hidden\" name=\"textid\" value=\"{$row['ID']}\"/>
                    <td><input type=\"submit\" name=\"view\" value=\"View\"/></td>
                </form> 
                <form method=\"POST\">
                    <input type=\"hidden\" name=\"textid\" value=\"{$row['ID']}\"/>
                    <td><input type=\"submit\" name=\"delete\" value=\"Delete\"/></td>
                </form>
               </tr>
               ";
                }
                echo"</table>";
            }
        }
        else if (isset($_POST['date']))
        {
            $_SESSION['date'] = $_POST['date'];
            unset($_SESSION['author_name']);
            include 'ClassLibs/statics.php';
            $rsResultPdf = rsPdfSearchByDate($_POST['date']);
            $rsResultText = rsTextSearchByDate($_POST['date']);
            echo"<h1>Pdf posts</h1>
                <table border=\"1\">
                <tr>
                <td>ID</td><td>Author name</td><td>File name</td><td>File type</td>
                <td>File size</td><td>Date</td><td>View</td><td>Delete</td>
                </tr>
                ";
            while ($row = mysql_fetch_assoc($rsResultPdf))
            {
                echo "
                <tr>
                    <td>{$row['ID']}</td><td>{$row['UserName']}</td><td>{$row['FileName']}</td>
                    <td>{$row['FileType']}</td><td>{$row['FileSize']}</td><td>{$row['Date']}</td>
                <form method=\"POST\" target=\"_blank\" action=\"ActionPdf.php\">
                  <input type=\"hidden\" name=\"pdfid\" value=\"{$row['ID']}\"/>
                  <td><input type=\"submit\" name=\"view\" value=\"View\"/></td>  
                </form>";
                if ($row['UserName'] === $_SESSION['userid'])
                {
                    echo"<form method=\"POST\">
                            <input type=\"hidden\" name=\"pdfid\" value=\"{$row['ID']}\"/>
                            <td><input type=\"submit\" name=\"delete\" value=\"Delete\"/></td>
                        </form>
                     </tr>";
                }
                else
                {
                    echo"</tr>";
                }
            }
            echo"</table><br/>";

            echo"<h1>Text posts</h1>
                <table border=\"1\">
                <tr>
                <td>ID</td><td>Author name</td><td>Date</td><td>View</td>
                <td>Delete</td>
                </tr>
                ";
            while ($row = mysql_fetch_assoc($rsResultText))
            {
                echo "
                <tr>
                    <td>{$row['ID']}</td><td>{$row['UserName']}</td><td>{$row['Date']}</td>
                <form method=\"POST\" target=\"_blank\" action=\"ActionText.php\">
                    <input type=\"hidden\" name=\"textid\" value=\"{$row['ID']}\"/>
                    <td><input type=\"submit\" name=\"view\" value=\"View\"/></td>
                </form>";
                if ($row['UserName'] === $_SESSION['userid'])
                {
                    echo "<form method=\"POST\">
                               <input type=\"hidden\" name=\"textid\" value=\"{$row['ID']}\"/>
                                <td><input type=\"submit\" name=\"delete\" value=\"Delete\"/></td>
                          </form>
                        </tr>";
                }
                else
                {
                    echo "</tr>";
                }
            }
            echo"</table>";
        }
        ?>
    </body>
</html>
